import requests
import json,jsonpath
import xlrd
import random,time
from config import *

class auto_reply:
    def __init__(self,username,password):
        workbook = xlrd.open_workbook(u"data/题目库.xlsx")
        self.sheet1 = workbook.sheet_by_name('Sheet1')
        self.s = requests.session()
        data['username'] = username
        data['password'] = password
        self.login()
    def login(self):
        Login_url = 'http://student.zjedu.moocollege.com/nodeapi/3.0.1/student/system/login'
        res = self.s.post(Login_url ,data=data,headers=headers)
        json1 = json.loads((str(res.text)))
        self.cookies ={'token':json1['data']['token']}

    def auto_delete_note(self):
        delete_list = "http://student.zjedu.moocollege.com/nodeapi/3.0.1/student/course/note/list"
        param = {
            'courseId':"30003052",
            'current': '1',
            'property': "updateTime",
            'chapterId':"",
            'favourite':'false',
            'sectionId':"",
            'my':'true'

        }
        res = self.s.post(delete_list,data=param,headers=headers,cookies=self.cookies)
        data = json.loads(res.text)
        pages  = int(jsonpath.jsonpath(data,'$..pages')[0])
        for i in range(pages):
            print("开始删除第"+str(i+1)+"页的笔记")
            self.delete_list()
    def delete_list(self):
        # 删除笔记功能
        delete_list = "http://student.zjedu.moocollege.com/nodeapi/3.0.1/student/course/note/list"
        param = {
            'courseId':"30003052",
            'current': '1',
            'property': "updateTime",
            'chapterId':"",
            'favourite':'false',
            'sectionId':"",
            'my':'true'

        }
        res = self.s.post(delete_list,data=param,headers=headers,cookies=self.cookies)
        data = json.loads(res.text)
        ids  = jsonpath.jsonpath(data,'$..id')
        print(ids)
        for id in ids:
            print(id)
            self.delete_note(id)
            

    def delete_note(self,id):
        delete = "http://student.zjedu.moocollege.com/nodeapi/3.0.1/student/course/note/delete"
        param = {
            "noteId":id
        }
        res = self.s.post(delete,data=param,cookies=self.cookies)
        if(res.status_code == 200):
            if(json.loads(res.text)['data']==True):
                print("笔记删除成功")

    def auto_response(self,count):
        '''
            发帖
            param: count 发帖量
        '''
        for i in range(1,count+1):
            content = self.sheet1.row_values(random.randrange(1,111))[0]
            data_wz = {
                'bbsId':'30014816',
                'content':'<p>'+content+'</p>',
                'courseId':courseId,
                'title':str(content)[:8],
                'topicId':''
            }
            headers ['Referer'] = 'http://student.zjedu.moocollege.com/course/study/30003052/discuss/form/30014816/add'
            headers ['Origin'] = 'http://student.zjedu.moocollege.com'
            fa_url = 'http://student.zjedu.moocollege.com/nodeapi/3.0.1/student/course/discuss/create'
            res = self.s.post(fa_url,data=data_wz,headers=headers,cookies=self.cookies)
            if res.status_code == 200 :
                print('本次发帖量',i)
            print('休息2秒钟')
            time.sleep(2)


    def auto_note(self,sectionId=0,start=30083390,end=30083434,mode=False,repetition=False):
        headers ['Referer'] = 'http://student.zjedu.moocollege.com/course/study/30003052/note/create'
        headers ['Origin'] = 'http://student.zjedu.moocollege.com'
        note_url = 'http://student.zjedu.moocollege.com/nodeapi/3.0.1/student/course/note/create'
        content = self.sheet1.row_values(random.randrange(1,111))[0]
        data = {
                'chapterId':'30083389',
                'courseId':courseId,
                'sectionId':sectionId,
                }
        if repetition:
            res = self.s.post('http://student.zjedu.moocollege.com/nodeapi/3.0.1/student/course/plan/note/list',headers=headers,data=data,cookies=self.cookies)
            total = str(json.loads(res.text)['data']['total'])
            if total != '0':
                print('笔记已经存在,跳过')
                return
        data['content']='<p>'+content+'</p>',
        data['title']=str(content)[:8]
        if(mode):
            self.s.post(note_url,data=data,headers=headers,cookies=self.cookies)
        else:
            lis = [i for i in range(start,end+1)]
            for i in lis:
               data['sectionId'] = i
               self.s.post(note_url,data=data,headers=headers,cookies=self.cookies)
        
        
    def auto(self,is_note=False):
        data ={
            'courseId':courseId
        }
        url = 'http://student.zjedu.moocollege.com/nodeapi/3.0.1/student/course/plan/list'
        headers ['Referer'] = 'http://student.zjedu.moocollege.com/course/study/30003052/plan'
        res = self.s.post(url,headers=headers,data=data,cookies=self.cookies)
        lis_json = json.loads(str(res.text))
        lis_1 = jsonpath.jsonpath(lis_json,'$..unitId')
        for unitId in lis_1:
            self.auto_vieo(unitId=unitId)
            print(unitId)

        if is_note :
            lis_2 = jsonpath.jsonpath(lis_json,'$..sectionId')
            for sectionId in lis_2:
                print('开始记笔记')
                self.auto_note(sectionId=sectionId,mode=True)
        
        

    def auto_vieo(self,unitId):
        url = 'http://student.zjedu.moocollege.com/nodeapi/3.0.1/student/course/uploadLearnRate'
        data = {
            'courseId':courseId,
            'playPosition': '0',
            'unitId':unitId
        }
        playPosition = [i * 20 for i in range(50)]
        for i in playPosition:
            data['playPosition'] = str(i)
            res = self.s.post(url,data = data,cookies = self.cookies)
            message =  json.loads(str(res.text))['message']
            if message == '参数校验失败':
                break
     
       










